//
//  RABookrackTopView.m
//  ReadApp
//
//  Created by 飞猪 on 2018/11/8.
//  Copyright © 2018年 Facebook. All rights reserved.
//

#import "RABookrackTopView.h"

@interface RABookrackTopView ()
<
ZYBannerViewDataSource,
ZYBannerViewDelegate,
KAAdNativeDelegate
>

@property (nonatomic, strong) ZYBannerView *banner;

@property (nonatomic, strong) UIView *adContainerView;
#pragma mark - Native广告
@property (nonatomic, strong) KAAdNative *ad;

@end

@implementation RABookrackTopView

#pragma mark - LifeCycle
- (instancetype)initWithFrame:(CGRect)frame {
  self = [super initWithFrame:frame];
  if (self) {
    [self createViews];
  }
  return self;
}

- (instancetype)initWithCoder:(NSCoder *)coder {
  self = [super initWithCoder:coder];
  if (self) {
    [self createViews];
  }
  return self;
}

- (void)layoutSubviews {
  [super layoutSubviews];
  
}

#pragma mark - CreateViews
- (void)createViews {
  self.backgroundColor = [UIColor whiteColor];
  
  [self setupBanner];
  
  self.ad = [[KAAdNative alloc] initWithSlot:@"QdADnkye" delegate:self];
  [self.ad load];
}

- (void)setupBanner {
  // 初始化
  self.banner = [[ZYBannerView alloc] init];
  self.banner.dataSource = self;
  self.banner.delegate = self;
  self.banner.autoScroll = YES;
  self.banner.shouldLoop = YES;
  [self addSubview:self.banner];
  
  // 设置frame
  [self.banner mas_makeConstraints:^(MASConstraintMaker *make) {
    make.edges.mas_equalTo(self);
  }];
}

- (void)setModels:(NSArray<RABookFaceModel *> *)models {
  _models = models;
  
  [self.banner reloadData];
}

- (NSArray<RABookFaceModel *> *)models {
  if (!_models) {
    _models = @[];
  }
  return _models;
}

#pragma mark - LazyLoad
- (UIView *)adContainerView {
  if (!_adContainerView) {
    _adContainerView = [[UIView alloc] init];
    _adContainerView.backgroundColor = UIColorHex(ebebeb);
  }
  return _adContainerView;
}

#pragma mark - ZYBannerViewDataSource
// 返回 Banner 需要显示 Item(View) 的个数
- (NSInteger)numberOfItemsInBanner:(ZYBannerView *)banner {
  return self.models.count + 1;
}

// 返回 Banner 在不同的 index 所要显示的 View (可以是完全自定义的v iew, 且无需设置 frame)
- (UIView *)banner:(ZYBannerView *)banner viewForItemAtIndex:(NSInteger)index {
  
  if (index == 0) {
    return self.adContainerView;
  }
  else {
    // 取出数据
    RABookFaceModel *model = [self.models objectAtIndex:index - 1];
    
    // 创建将要显示控件
    UIImageView *imageView = [[UIImageView alloc] init];
    imageView.userInteractionEnabled = YES;
    [imageView sd_setImageWithURL:[NSURL URLWithString:model.iconUrl] placeholderImage:[UIImage imageWithColor:UIColorHex(ebebeb)]]; // @"https://goss3.vcg.com/editorial/vcg/800/new/VCG111175433278.jpg"
    imageView.contentMode = UIViewContentModeScaleAspectFill;
    [imageView addGestureRecognizer:[[UITapGestureRecognizer alloc] initWithActionBlock:^(id  _Nonnull sender) {
      [self jump:model];
    }]];
    
    return imageView;
  }
  
}

- (void)jump:(RABookFaceModel *)model {
  
  if (self.jumpToBookDetail) {
    self.jumpToBookDetail(model);
  }
  
}

#pragma mark - KAAdNativeDelegate
// 广告请求成功
- (void)nativeAdRequestCompletedWithAd:(nonnull KAAdNative *)nativeAd {
  
  UIView *primaryView = [nativeAd primiaryViewOfSize:CGSizeMake(kScreenWidth, kScreenWidth * (640.0 / 960.0))];
  [self.adContainerView addSubview:primaryView];
  [self.banner reloadData];
  
  [nativeAd nativeAdRenderedWithView:primaryView]; // 上报广告展示
}

// 广告请求失败
- (void)nativeAdRequestFailedForSlot:(nonnull NSString *)nativeAdSlot
                          withStatus:(nonnull NSError *)nativeAdStatus {
  [self.ad load];
}

- (void)nativeAdRequestFailedWithAd:(nonnull KAAdNative *)nativeAd
                         withStatus:(nonnull NSError *)nativeAdStatus {}

#pragma mark - ZYBannerViewDelegate
// 在这里实现点击事件的处理
- (void)banner:(ZYBannerView *)banner didSelectItemAtIndex:(NSInteger)index {
  NSLog(@"点击了第%ld个项目", (long)index);
}

- (void)banner:(ZYBannerView *)banner didScrollToItemAtIndex:(NSInteger)index {
  NSLog(@"滚动到第%ld个项目", (long)index);
  
}

@end
